import { Injectable, NotFoundException } from '@nestjs/common';
import { CreateScenePathRelationalDto } from './dto/create-scene_path_relational.dto';
import { UpdateScenePathRelationalDto } from './dto/update-scene_path_relational.dto';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { ScenePathRelational } from './entities/scene_path_relational.entity';
import { FileListService } from '../file-list/file-list.service';
import { PathListService } from '../path-list/path-list.service';

@Injectable()
export class ScenePathRelationalService {
  constructor(
    @InjectRepository(ScenePathRelational)
    private readonly scenePathRelationalPaRepository: Repository<ScenePathRelational>,
    private readonly fileListService: FileListService,
    private readonly pathListService: PathListService,
  ) {}
  create(createScenePathRelationalDto: CreateScenePathRelationalDto) {
    const scenePathRelational = this.scenePathRelationalPaRepository.create(
      createScenePathRelationalDto,
    );
    this.scenePathRelationalPaRepository.insert(scenePathRelational);
    return ;
  }

  async findAll(): Promise<ScenePathRelational[]> {
    return this.scenePathRelationalPaRepository.find();
  }

  async getPathBySceneId(scene_id: string): Promise<ScenePathRelational[]> {
    return this.scenePathRelationalPaRepository.find({
      where: { scene_id: scene_id },
      select: ['path_id'],
    });
  }

  async findBySceneId(scene_id: string): Promise<ScenePathRelational[]> {
    return this.scenePathRelationalPaRepository.find({
      where: { scene_id: scene_id },
      select: ['path_id'],
    });
  }

  async getPathByPathIdsAndTypeBySceneId(scene_id: string, fileType: string) {
    const pathList = await this.scenePathRelationalPaRepository.find({
      where: { scene_id: scene_id },
      select: ['path_id'],
    });
    if (pathList.length === 0) return [];
    const pathIdList = pathList.map((item) => item.path_id);
    const fileList = await this.pathListService.getPathByPathIdsAndType(
      fileType,
      pathIdList,
    );
    return fileList;
  }

  async getPathByPathIdsBySceneId(scene_id: string) {
    const pathList = await this.scenePathRelationalPaRepository.find({
      where: { scene_id: scene_id },
      select: ['path_id'],
    });
    if (pathList.length === 0) return [];
    const pathIdList = pathList.map((item) => item.path_id);
    const pathListResult =
      await this.pathListService.getPathByPathIds(pathIdList);
    return pathListResult;
  }

  async existsBySceneIdAndPathId(
    scene_id: string,
    path_id: string,
  ): Promise<boolean> {
    const count = await this.scenePathRelationalPaRepository.count({
      where: { scene_id: scene_id, path_id: path_id },
    });
    return count > 0;
  }

  async findOne(id: string): Promise<ScenePathRelational> {
    return this.scenePathRelationalPaRepository.findOne({ where: { id: id } });
  }

  async update(
    id: string,
    updateScenePathRelationalDto: UpdateScenePathRelationalDto,
  ): Promise<ScenePathRelational | string> {
    const oldItem = await this.scenePathRelationalPaRepository.findOne({
      where: { id },
    });

    if (!oldItem) {
      throw new NotFoundException('该修改对象不存在');
    }

    const updateScenePathRelational =
      this.scenePathRelationalPaRepository.merge(
        oldItem,
        updateScenePathRelationalDto,
      );

    await this.scenePathRelationalPaRepository.save(updateScenePathRelational);

    // 返回更新后的实体或其他有用的信息
    return updateScenePathRelational;
  }

  async remove(id: string) {
    const result = await this.scenePathRelationalPaRepository.delete({
      id: id,
    });
    if (result.affected && result.affected > 0) {
      return `成功删除 ${result.affected} 条记录`;
    } else {
      return `未找到匹配的记录，没有进行删除操作`;
    }
  }

  async removeByPathId(pathId: string, sceneId: string) {
    const result = await this.scenePathRelationalPaRepository.delete({
      path_id: pathId,
      scene_id: sceneId,
    });
    if (result.affected && result.affected > 0) {
      return `成功删除 ${result.affected} 条记录`;
    } else {
      return `未找到匹配的记录，没有进行删除操作`;
    }
  }
}
